/**
 * @constructor
 * @param {number|undefined} val
 * @param {ListNode|undefined} next
 * @return {{ val: number, next: ListNode|null }}
 */

function ListNode(val = undefined, next = undefined) {
  this.val = (val === undefined ? 0 : val);
  this.next = (next === undefined ? null : next);
}
exports.ListNode = ListNode;

/**
 * @param {ListNode} listNode
 * @return {string}
 */
exports.displayListNode = function displayListNode(listNode) {
  const list = [];
  let cur = listNode;
  while (cur) {
    list.push(cur.val);
    cur = cur.next;
  }
  return `[${list.join(',')}]`;
};

/**
 * @param {number[]} arr
 * @return {ListNode|null}
 */
function createListNode(arr) {
  if (!arr.length) return null;
  const result = new ListNode(arr[0]);
  let cur = result;
  for (let i = 1; i < arr.length; i += 1) {
    cur.next = new ListNode(arr[i]);
    cur = cur.next;
  }
  return result;
}
exports.createListNode = createListNode;

/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
exports.findListNodeByVal = function findListNodeByVal(head, val) {
  let node = head;
  while (node) {
    if (node.val === val) {
      break;
    }
    node = node.next;
  }
  return node;
};

/**
 * @param {ListNode} head
 * @param {number} index
 * @return {ListNode}
 */
function findListNodeByIndex(head, index) {
  let node = head;
  let i = 0;
  while (node && i < index) {
    node = node.next;
    i += 1;
  }
  return node;
}
exports.findListNodeByIndex = findListNodeByIndex;

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
function getListNodeTail(head) {
  if (!head) {
    return head;
  }
  let node = head;
  while (node.next) {
    node = node.next;
  }
  return node;
}
exports.getListNodeTail = getListNodeTail;

/**
 * @param {number[]} arr
 * @param {number} index
 * @return {ListNode}
 */
exports.createCycleListNode = function createCycleListNode(arr, index) {
  const head = createListNode(arr);
  if (!head) {
    return head;
  }
  getListNodeTail(head).next = findListNodeByIndex(head, index);
  return head;
};

/**
 * @param {(number|string|boolean)[]} arr
 */
exports.displayBaseArray = function displayBaseArray(arr) {
  return `[${arr.join(',')}]`;
};

/**
 * @param {(any)[]} arr
 */
exports.displayNestedArray = function displayNestedArray(arr) {
  return `[${
    arr.map(
      (item) => (Array.isArray(item) ? displayNestedArray(item) : item),
    )
  }]`;
};

/**
 * @constructor
 * @param {number|undefined} val
 * @param {TreeNode|undefined} left
 * @param {TreeNode|undefined} right
 * @return {{ val: number, left: TreeNode|null, right: TreeNode|null }}
 */
function TreeNode(val = undefined, left = undefined, right = undefined) {
  this.val = (val === undefined ? 0 : val);
  this.left = (left === undefined ? null : left);
  this.right = (right === undefined ? null : right);
}
exports.TreeNode = TreeNode;

/**
 * @param {(number|null)[]} arr
 * @return {TreeNode|null}
 */
exports.createTreeNode = function createTreeNode(arr) {
  if (!arr.length || arr[0] === null) return null;
  const root = new TreeNode(arr[0]);
  const stack = [root];
  let i = 1;
  while (stack.length && i < arr.length) {
    const node = stack.shift();
    const left = (i < arr.length && arr[i] !== null) ? new TreeNode(arr[i]) : null;
    i += 1;
    const right = (i < arr.length && arr[i] !== null) ? new TreeNode(arr[i]) : null;
    i += 1;
    node.left = left;
    node.right = right;
    if (node.left) {
      stack.push(node.left);
    }
    if (node.right) {
      stack.push(node.right);
    }
  }
  return root;
};

/**
 * @param {TreeNode} root
 * @return {string}
 */
exports.displayTreeNode = function displayTreeNode(root) {
  if (!root) return '[]';
  const stack = [root];
  const result = [];
  while (stack.length) {
    const node = stack.shift();
    if (node) {
      result.push(node.val);
      stack.push(node.left, node.right);
    } else {
      result.push(null);
    }
  }
  while (result[result.length - 1] === null) {
    result.pop();
  }
  return `[${result.reduce((total, val, index) => {
    const str = val === null || val === undefined ? 'null' : String(val);
    if (index === 0) {
      return str;
    }
    return `${total},${str}`;
  }, '')}]`;
};

/**
 * @param {TreeNode} root
 * @param {number} val
 * @return {TreeNode}
 */
exports.findTreeNode = function findTreeNode(root, val) {
  if (!root) return null;
  if (root.val === val) {
    return root;
  }
  let result = findTreeNode(root.left, val);
  if (result) {
    return result;
  }
  result = findTreeNode(root.right, val);
  if (result) {
    return result;
  }
  return null;
};
